const tree = [
    {
        id: 0,
        name: 'root',
        children: [
            {
                id: 1,
                name: 'child1',
                children: [
                    {
                        id: 4,
                        name: 'child1-1',
                        children: []
                    },
                    {
                        id: 5,
                        name: 'child1-2',
                        children: []
                    }
                ]
            },
            {
                id: 2,
                name: 'child2',
                children: [
                    {
                        id: 6,
                        name: 'child2-1',
                        children: [
                            {
                                id: 8,
                                name: 'child2-1-1',
                                children: []
                            }
                        ]
                    }
                ]
            },
            {
                id: 3,
                name: 'child3',
                children: [
                    {
                        id: 7,
                        name: 'child3-1',
                        children: []
                    }
                ]
            }
        ]
    }
]


//转成列表
function treeToList(tree,result = [],parent=null){
    tree.forEach(item => {
        item.parentId = parent

        result.push(item)
        //查看有没子节点
        if(item.children.length!==0){
            treeToList(item.children,result,item.id);
         
        }
           //删除
           delete item.children
    });

    return result;
}
const list = treeToList(tree);

function listToMap(list,result={}) {
    // list.forEach(item => {
    //     result[item.id] = item
    // });

    return list.reduce((prev,curr) => ((prev[curr.id] = curr),prev),{})

    
}

const map = listToMap(list);
console.log(map);